<!--- Document Information -----------------------------------------------------

Title:      HibernateFactory.cfc

Author:     Mark Mandel
Email:      mark@compoundtheory.com

Website:    http://www.compoundtheory.com

Purpose:    Factory for getting to Hibernate objects

Usage:      

Modification Log:

Name			Date			Description
================================================================================
Mark Mandel		27/06/2006		Created

------------------------------------------------------------------------------->

<cfcomponent name="HibernateObject" hint="Factory for getting to Hibernate objects">

<cfscript>
	instance = StructNew();
</cfscript>

<cffunction name="init" hint="Constructor: returns this" access="public" returntype="HibernateFactory" output="false">
	<cfargument name="hibernateObject" hint="hibernate object" type="any" required="Yes">
	<cfscript>
		variables.hibernateObject = arguments.hibernateObject;
		return this;
	</cfscript>
</cffunction>

<!------------------------------------------- PUBLIC ------------------------------------------->

<cffunction name="load" hint="returns hibernate config object" access="public" returntype="any" output="false">
	<cfset var 	loaded = "" />
	<cftry>
		<cfset loaded = variables.hibernateObject.load(=);
	</cftry>
</cffunction>


<!------------------------------------------- PACKAGE ------------------------------------------->

<!------------------------------------------- PRIVATE ------------------------------------------->

<cffunction name="throwerror">
  <cfargument name="type" required="true" />
  <cfargument name="message" default="" />
	<cfthrow type="#arguments.type#" message="#arguments.message#" />
</cffunction>

<cffunction name="dumpvar">
  <cfargument name="var">
  <cfargument name="abort" default="true">
  <cftry>
<!---
 		<cfset class = createObject("java","java.lang.Class") />
		<cfset meths = var.getDeclaredMethods() />
		<cfset ms = structNew() />
		<cfloop from="1" to="#arrayLen(meths)#" index="m">
		  <cfset ms[meths[m].toString()] = structNew() />
		  <cfset ms[meths[m].toString()]["public"] = meths[m].public>
		  <cfset mps = meths[m].getParameterTypes()>
			<cfloop from="1" to="#arrayLen(mps)#" index="p">
			  <cfset ms[meths[m].toString()][mps[p].getName()] = mps[p].getName() />
			</cfloop>
		</cfloop>
	  <cfdump var="#ms#" />
--->
	  <cfdump var="#var#" />
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>

<cffunction name="createJavaProxy" hint="create a javaproxy, dependent on CF server settings" access="private" returntype="any" output="false">
	<cfargument name="class" hint="the java class to create the proxy with" type="any" required="Yes">
	<cfscript>
		try
		{
			return createObject("java", "coldfusion.runtime.java.JavaProxy").init(arguments.class);
		}
		catch(Object exc)
		{
			return createObject("component", "actionpacks.cfhibernate.ext.javaloader.JavaProxy")._init(arguments.class);
		}
	</cfscript>
</cffunction>

<cffunction name="dumpPackages" hint="Inititalises all the java libs to be loaded in" access="public" returntype="void" output="false">
	<cfscript>
		var package = createObject("java","java.lang.Package");
		var packages = package.getPackages();
		var allpacks = arrayNew(1);
		var pack = "";
		var packinfo = "";
		for (x = 1; x lte arrayLen(packages); x = x+1) {
			pack = packages[x];
			packinfo = structNew();
			packinfo["name"] = pack.getName();
			if(len(pack.getImplementationVersion())) {
				packinfo["version"] = pack.getImplementationVersion();
			}
			if(len(pack.getSpecificationVersion())) {
				packinfo["specver"] = pack.getSpecificationVersion();
			}
			if(len(pack.getSpecificationTitle())) {
				packinfo["title"] = pack.getSpecificationTitle();
			}
				
			allpacks[x] = packinfo;
		}
		dumpvar(allpacks);
	</cfscript>
</cffunction>

<cffunction name="loadDirectory" hint="Loads the JAR file paths into the array" access="public" returntype="array" output="false">
	<cfargument name="path" hint="The path to oad" type="string" required="Yes">
	<cfargument name="library" hint="The array of classpaths" type="array" required="Yes">
	<cfscript>
		var qJars = 0;
	</cfscript>
	<cfdirectory action="list" directory="#arguments.path#" filter="*.jar" name="qJars">	
	<cfloop query="qJars">
		<cfscript>
			ArrayAppend(arguments.library, arguments.path & name);
		</cfscript>
	</cfloop>	
	<cfscript>
		return arguments.library;
	</cfscript>
</cffunction>

<cffunction name="getJavaLoader" access="private" returntype="any" output="false">
	<cfreturn variables.JavaLoader />
</cffunction>

<cffunction name="setJavaLoader" access="private" returntype="void" output="false">
	<cfargument name="JavaLoader" type="any" required="true">
	<cfset variables.JavaLoader = arguments.JavaLoader />
</cffunction>

<cffunction name="makeLoader" access="private" returntype="void" output="false">
	<cfargument name="jarsarray" required="true" type="array" />
	<cfscript>
		var Class = createObject("java", "java.lang.Class");
		var Array = createObject("java", "java.lang.reflect.Array");
		var iterator = arguments.jarsarray.iterator();
		var file = 0;
		var urls = Array.newInstance(Class.forName("java.net.URL"), ArrayLen(jarsarray));
		var counter = 0;
//		dumpvar(jarsarray);
	try {
		while(iterator.hasNext())
		{
			Array.set(urls, javacast("int",counter), newFile(iterator.next()).toURL());
			counter = counter + 1;
		}

		classLoader = createObject("java", "java.net.URLClassLoader").init(urls);
	}
	catch(Any excpt) {
		throwerror("cfhibernate.HibernateFactory","de Schema Genarator Error:" & excpt.message & " " & excpt.stacktrace);
	}

//		classLoader = createObject("java", "ChildFirstClassLoader").init(urls);
		setJavaLoader(classLoader);		
	</cfscript>
</cffunction>

</cfcomponent>